package com.bf.electroplating.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bf.electroplating.mapper.WasteGasMonitoringMapper;
import com.bf.electroplating.pojo.entity.*;
import com.bf.electroplating.pojo.entity.WasteGasMonitoring;
import com.bf.electroplating.pojo.enums.MachineStatusEnums;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.wasteGasMonitoringVO.WasteGasExcelExportDTO;
import com.bf.electroplating.pojo.vo.wasteGasMonitoringVO.WasteGasMonitoringDetailDTO;
import com.bf.electroplating.pojo.vo.wasteGasMonitoringVO.WasteGasMonitoringQueryDTO;
import com.bf.electroplating.pojo.vo.wasteGasMonitoringVO.WasteGasMonitoringVO;
import com.bf.electroplating.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 废气处理设施检测表 服务实现类
 * </p>
 *
 * @author gzd
 * @since 2025-08-22
 */
@Service
public class WasteGasMonitoringServiceImpl extends ServiceImpl<WasteGasMonitoringMapper, WasteGasMonitoring> implements IWasteGasMonitoringService {

    @Resource
    private IWasteGasMonitoringLogService wasteGasMonitoringLogService;


    @Override
    public ResultVO < Page <WasteGasMonitoringVO> > pageList (WasteGasMonitoringQueryDTO queryDTO ) {
        Page <WasteGasMonitoring> page = new Page <> ( queryDTO.getPageNum () , queryDTO.getPageSize () );

        LambdaQueryWrapper< WasteGasMonitoring > wrapper = new LambdaQueryWrapper <> ();

        if ( org.apache.commons.lang3.StringUtils.isNotBlank ( queryDTO.getCompanyId () ) ) {
            wrapper.eq ( WasteGasMonitoring::getCompanyId , queryDTO.getCompanyId () );
        }

        if ( StringUtils.isNotBlank ( queryDTO.getMnCode () ) ) {
            wrapper.like ( WasteGasMonitoring::getMnCode , queryDTO.getMnCode () );
        }

        if ( !CollectionUtils.isEmpty(queryDTO.getTimes () ) ) {
            wrapper.ge ( WasteGasMonitoring::getMonitoringTime , queryDTO.getTimes ().get ( 0 ) )
                    .le(WasteGasMonitoring::getMonitoringTime , queryDTO.getTimes ().get ( 1 ) );
        }


        wrapper.orderByDesc ( WasteGasMonitoring::getCreateTime );

        Page < WasteGasMonitoring > resultPage = this.page ( page , wrapper );

        // 转换为VO
        Page < WasteGasMonitoringVO > voPage = new Page <> ();
        BeanUtils.copyProperties ( resultPage , voPage , "records" );
        List< WasteGasMonitoringVO > voList = new ArrayList<>();
        for ( WasteGasMonitoring entity : resultPage.getRecords () ) {
            WasteGasMonitoringVO vo = new WasteGasMonitoringVO ();
            BeanUtils.copyProperties ( entity , vo );
            vo.setMonitoringTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(entity.getMonitoringTime()));
            vo.setStatus(MachineStatusEnums.getDesc(entity.getStatus()));
            voList.add ( vo );
        }
        voPage.setRecords ( voList );
        return ResultVO.ok ( voPage );
    }





    @Override
    public ResultVO < WasteGasMonitoringVO > getDetail (WasteGasMonitoringDetailDTO dto ) {
        if ( StringUtils.isBlank ( dto.getId() ) ) {
            return ResultVO.fail ( "ID不能为空" );
        }
        WasteGasMonitoring entity = this.getById ( dto.getId() );
        if ( entity == null ) {
            return ResultVO.fail ( "记录不存在" );
        }
        WasteGasMonitoringVO vo = new WasteGasMonitoringVO ();
        BeanUtils.copyProperties ( entity , vo );
        Page <WasteGasMonitoringLog> page = new Page <> ( dto.getPageNum () , dto.getPageSize () );
        LambdaQueryWrapper<WasteGasMonitoringLog>  wrapper = new LambdaQueryWrapper <> ();
        wrapper.eq(WasteGasMonitoringLog::getMonitoringId,entity.getId());
        if (!CollectionUtils.isEmpty( dto.getTimes () ) &&dto.getTimes().get(0)!=null) {
            wrapper .ge(WasteGasMonitoringLog::getMonitoringTime ,  dto.getTimes ().get ( 0 ) );
        }
        if (!CollectionUtils.isEmpty( dto.getTimes () ) &&dto.getTimes().get(1)!=null) {
            wrapper.le(WasteGasMonitoringLog::getMonitoringTime , dto.getTimes ().get ( 1 ));
        }
        wrapper.orderByDesc( WasteGasMonitoringLog::getMonitoringTime );
        Page<WasteGasMonitoringLog> wasteGasMonitoringLogs=wasteGasMonitoringLogService.page(
                page, wrapper
        );
        List<WasteGasExcelExportDTO> voList=wasteGasMonitoringLogs.getRecords().stream().map(i->{
            WasteGasExcelExportDTO wasteGasExcelExportDTO = new WasteGasExcelExportDTO ();
            BeanUtils.copyProperties ( i , wasteGasExcelExportDTO );
            wasteGasExcelExportDTO.setMonitoringTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(i.getMonitoringTime()));
            wasteGasExcelExportDTO.setStatus(MachineStatusEnums.getDesc(i.getStatus()));
            return wasteGasExcelExportDTO;
        }).toList();
        Page < WasteGasExcelExportDTO > voPage = new Page <> ();
        BeanUtils.copyProperties ( wasteGasMonitoringLogs , voPage , "records" );
        voPage.setRecords ( voList );
        vo.setDetails(voPage);
        vo.setStatus(MachineStatusEnums.getDesc(vo.getStatus()));
        return ResultVO.ok ( vo );
    }

    @Override
    public void export(WasteGasMonitoringDetailDTO dto, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 防止中文乱码
        String fileName = URLEncoder.encode("检测详情", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        WasteGasMonitoring entity = this.getById ( dto.getId() );
        LambdaQueryWrapper<WasteGasMonitoringLog>  wrapper = new LambdaQueryWrapper <> ();
        wrapper.eq(WasteGasMonitoringLog::getMonitoringId,entity.getId());
        if (!CollectionUtils.isEmpty( dto.getTimes () ) &&dto.getTimes().get(0)!=null) {
            wrapper .ge(WasteGasMonitoringLog::getMonitoringTime ,  dto.getTimes ().get ( 0 ) );
        }
        if (!CollectionUtils.isEmpty( dto.getTimes () ) &&dto.getTimes().get(1)!=null) {
            wrapper.le(WasteGasMonitoringLog::getMonitoringTime , dto.getTimes ().get ( 1 ));
        }
        wrapper.orderByDesc( WasteGasMonitoringLog::getMonitoringTime );
        List<WasteGasMonitoringLog> wasteGasMonitoringLogs=wasteGasMonitoringLogService.list(
                 wrapper
        );
        List<WasteGasExcelExportDTO> voList=wasteGasMonitoringLogs.stream().map(i->{
            WasteGasExcelExportDTO wasteGasExcelExportDTO = new WasteGasExcelExportDTO ();
            BeanUtils.copyProperties ( i , wasteGasExcelExportDTO );
            wasteGasExcelExportDTO.setMonitoringTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(i.getMonitoringTime()));
            wasteGasExcelExportDTO.setStatus(MachineStatusEnums.getDesc(i.getStatus()));
            return wasteGasExcelExportDTO;
        }).toList();

        EasyExcel.write(response.getOutputStream(), WasteGasExcelExportDTO.class)
                .sheet("用户信息")
                .doWrite(voList);
    }

}
